<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <input type="text" name="username" class="user">
    <br>
    <input type="password" name="password" class="pwd">
    <br>
    <button type="submit" class="btn">登陆</button>

    <script src="../js/ajax.js"></script>
    <script>

        // 登陆成功的时候，需要把用户名的登陆状态存储在localStorage中


        const get = ele => document.querySelector(ele);
        get('.btn').onclick = function () {
            // 获取用户名和密码
            const username = get('.user').value;
            const password = get('.pwd').value;
            // 验证
            // 请求
            ajax({
                type: 'post',
                path: 'http://43.138.81.225/php/login.php',
                data: {
                    username,
                    password
                },
                success: res => {
                    console.log(res);
                    const { status, msg } = res;
                    if(status) {
                        alert('登陆成功');
                        // 需要把登陆状态保存到localStorage中
                        localStorage.setItem('USER_LOGIN' , username);
                        // 登陆成功，跳转--- 从哪里来回哪里去

                        // 判断来源
                        let str = location.search ;
                        if(str) {
                            console.log(str);
                            let url = str.replace('?historyURL=' , '');
                            location.href = url ;
                        } else {
                            // 如果没有来源，默认跳转到首页
                            location.href = './04_轮播v2.html';
                        }
                        
                    } else {
                        alert(msg);
                        
                    }
                }
            })
        }

    </script>

</body>

</html>